package com.apes.fico.finance.respository;

import com.apes.fico.finance.model.costcollect.ProportionServeType;
import com.apes.framework.jpa.repository.CustomRepository;
import com.apes.scm.masterdata.frame.model.Dept;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Date;
import java.util.List;
import java.util.Map;

public interface ProportionServeTypeRepository extends CustomRepository<ProportionServeType, String> {

    @Query(value = "SELECT * FROM GL_PROPORTION_SERVE_TYPE A " +
            "WHERE EXTRACT(MONTH FROM A.YEAR_MONTH) = :months " +
            "AND EXTRACT(YEAR FROM A.YEAR_MONTH) = :years " +
            "AND A.DEPT_ID = :deptId",nativeQuery = true)
    List<ProportionServeType> findByYearMonthAndDept(@Param("months") String months, @Param("years") String years, @Param("deptId") String deptId);

    @Query(value = "SELECT T.deptId, SUM(T.SGTC) SGTC, T.serveTypeId " +
            "  FROM (SELECT A.DEPT_ID AS deptId, " +
            "               SUM(A.TC_AMT) AS SGTC, " +
            "               A.SERVE_TYPE_ID AS serveTypeId " +
            "          FROM TC_RYSGTC A " +
            "         WHERE EXTRACT(MONTH FROM A.TJRQ) = :months " +
            "           AND EXTRACT(YEAR FROM A.TJRQ) = :years " +
            "         GROUP BY A.DEPT_ID, A.SERVE_TYPE_ID) T " +
            " WHERE T.SGTC <> 0 " +
            " GROUP BY T.deptId, T.serveTypeId",nativeQuery = true)
    List<Map<String,Object>> findSgTc(@Param("months") String months, @Param("years") String years);

    @Query(value = "SELECT T.deptId " +
            "  FROM (SELECT A.DEPT_ID AS deptId, " +
            "               SUM(A.TC_AMT) AS SGTC, " +
            "               A.SERVE_TYPE_ID AS serveTypeId " +
            "          FROM TC_RYSGTC A " +
            "         WHERE EXTRACT(MONTH FROM A.TJRQ) = :months " +
            "           AND EXTRACT(YEAR FROM A.TJRQ) = :years " +
            "         GROUP BY A.DEPT_ID, A.SERVE_TYPE_ID) T " +
            " WHERE T.SGTC <> 0 " +
            " GROUP BY T.deptId",nativeQuery = true)
    List<Map<String,Object>> findDeptId(@Param("months") String months, @Param("years") String years);

}
